package com.example.yin.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 *
 * 前端运行在 http://localhost:8080
 *
 * 后端 API 运行在 http://localhost:8888
 *
 * 需要处理跨域请求
 **/
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Bean //依赖注入拦截器对象
    public CorsInterceptor corsInterceptor() {
        return new CorsInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(corsInterceptor())
                .addPathPatterns("/**");
    }
/*注册自定义拦截器到 Spring MVC 中

addInterceptor(corsInterceptor())：添加之前定义的 CORS 拦截器

.addPathPatterns("/**")：指定拦截器作用于所有路径*/
}
